package com.woniu.gf.pawn.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.gf.pawn.comm.core.domain.ProductBrand;
import com.woniu.gf.pawn.comm.core.domain.ProductBrandBigType;
import com.woniu.gf.pawn.comm.core.dto.ResultDto;
import com.woniu.gf.pawn.product.parma.*;

import com.woniu.gf.pawn.service.IProductBrandService;
import com.woniu.gf.pawn.utils.BindingResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-10-15
 */
@RestController
@RequestMapping("/product-brand")
@Api(tags = "品牌的控制类")
public class ProductBrandController {

    @Autowired
    private IProductBrandService productBrandService;

    /**
     * 添加
     * @param addProductBrandParam
     * @return
     */
    @PostMapping("addBrand")
    @ApiOperation("添加品牌")
    public ResultDto addBrand(@RequestBody @Valid AddProductBrandParam addProductBrandParam, BindingResult result){
         if(addProductBrandParam.getBigIds().size()==0)return ResultDto.fail(202,"品牌新增失败","大类id为空");
        String bindingResult = BindingResultUtils.getBindingResult(result);
        if(bindingResult.isEmpty()) {
            ProductBrand productBrand = new ProductBrand();
            BeanUtils.copyProperties(addProductBrandParam,productBrand);

            List<ProductBrandBigType> productBrandBigTypeList=new ArrayList<>();
            for (int i=0;i<addProductBrandParam.getBigIds().size();i++){
                ProductBrandBigType productBrandBigType = new ProductBrandBigType();
                productBrandBigType.setProductBigTypeId(addProductBrandParam.getBigIds().get(i));
                productBrandBigTypeList.add(productBrandBigType);
            }
            productBrandService.addBrand(productBrand,productBrandBigTypeList);
            return ResultDto.success(200,"品牌新增成功");
        }
        return ResultDto.fail(202,"品牌新增失败",result);

    }

    /**
     * 查询
     * @param productBrandPageParam
     * @return
     */
    @ApiOperation("查询以及模糊查询品牌")
    @GetMapping("findPage")
    public ResultDto findPage(@Valid FindProductBrandPageParam productBrandPageParam,BindingResult result){
        String bindingResult = BindingResultUtils.getBindingResult(result);
        if(bindingResult.isEmpty()) {
            IPage<ProductBrand> productBrandIPage = productBrandService.selectPageVo(productBrandPageParam);
            return ResultDto.success(200, "查询品牌", productBrandIPage);
        }
        return ResultDto.success(200, "查询失败", null);
    }

    /**
     * 查询品牌拥有的大类
     * @param findBigBrandParam
     * @return
     */
    @GetMapping("findById")
    @ApiOperation("查询品牌拥有的大类")
    public ResultDto findById(@Valid FindBigBrandParam findBigBrandParam,BindingResult result){
        String bindingResult = BindingResultUtils.getBindingResult(result);
        if(bindingResult.isEmpty()) {
            List<Map<Integer, String>> map = productBrandService.findMapById(findBigBrandParam);
            return ResultDto.success(200, "查询品牌拥有的大类", map);
        }
        return ResultDto.fail(202, "查询品牌拥有的大类失败", "参数为空");
    }

    /**
     * 查询品牌不拥有的大类
     * @param findBigBrandParam
     * @return
     */
    @ApiOperation("查询品牌不拥有的大类")
    @GetMapping("findNotById")
    public ResultDto findNotById(@Valid FindBigBrandParam findBigBrandParam,BindingResult result){
        String bindingResult = BindingResultUtils.getBindingResult(result);
        if(bindingResult.isEmpty()) {
            List<Map<Integer, String>> map = productBrandService.findNotMapById(findBigBrandParam);
            return ResultDto.success(200, "查询品牌不拥有的大类", map);
        }
        return ResultDto.fail(202, "查询品牌不拥有的大类失败", "参数为空");
    }

    /**
     * 修改
     * @param updateBrandParam
     * @return
     */
    @ApiOperation("修改品牌")
    @PostMapping("updateBrand")
    public ResultDto updateBrand(@RequestBody @Valid UpdateBrandParam updateBrandParam,BindingResult result){
        String bindingResult = BindingResultUtils.getBindingResult(result);
        if(bindingResult.isEmpty()) {
            ProductBrand productBrand = new ProductBrand();
            BeanUtils.copyProperties(updateBrandParam, productBrand);
            List<ProductBrandBigType> productBrandBigTypeList = new ArrayList<>();
            for (int i = 0; i < updateBrandParam.getBigId().size(); i++) {
                ProductBrandBigType productBrandBigType = new ProductBrandBigType();
                productBrandBigType.setProductBigTypeId(updateBrandParam.getBigId().get(i));
                productBrandBigType.setProductBrandId(updateBrandParam.getProductBrandId());
                productBrandBigTypeList.add(productBrandBigType);
            }
            productBrandService.updateBrand(productBrand, productBrandBigTypeList);
            return ResultDto.success(200, "修改品牌成功");
        }
        return ResultDto.fail(202, "修改品牌失败");
    }

    /**
     * 删除品牌
     */
    @ApiOperation("删除品牌")
    @PostMapping("deleteBrand")
    public  ResultDto  deleteBrand(@RequestBody ListIdParam listIdParam)  {

        if(listIdParam.getIds()==null)  return  ResultDto.fail(202,"删除失败","参数为空");
        productBrandService.deleteBrand(listIdParam.getIds());
        return  ResultDto.success(200,"删除成功");
    }

}

